import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.websocket.WebSocket;
import org.eclipse.jetty.websocket.WebSocketServlet;

import Game.Game;
import Game.GameManager;
import Joueur.ActionPointException;

/**
 * Servlet implementation class ClientActionServlet
 */
public class ClientActionServlet extends WebSocketServlet 
{
	private static final long serialVersionUID = 1L;

	/**
	 * Default constructor. 
	 */
	public ClientActionServlet() 
	{
		System.out.println("Servlet Created");
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		System.out.println("doGet");
	}

	@Override
	public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol) 
	{
		System.out.println("doWebSocketConnect");
		return new ClientActionSocket();
	}

	class ClientActionSocket implements WebSocket.OnTextMessage 
	{
		private Connection _connection;

		public ClientActionSocket() 
		{
			System.out.println("webSocketCreation");
		}

		public void onMessage(String data) 
		{
			System.out.println("onMessage: " + data);   
			String[] mr = data.split("\\s+");
			String ms = "undefined";

			String action = mr[0];

			switch (action)
			{
			case "join": ms = joinGame(); break;
			case "move": ms = move(mr[1], mr[2], mr[3], mr[4]); break;
			case "pass": ms = passTurn(mr[1]); break;
			case "quit": ms = quitGame(mr[1], mr[2]); break;
			}

			try 
			{
				System.out.println(ms);
				_connection.sendMessage(ms);
			} 

			catch (IOException e) 
			{
				e.printStackTrace();
			}
		}

		private String quitGame(String gameId, String playerId) 
		{
			Game game = GameManager.getInstance().getGame(Integer.parseInt(gameId));
			game.remove(Integer.parseInt(playerId));
			return "quitOK";
		}

		private String move(String gameId, String legionId, String x, String y) 
		{
			Game g = GameManager.getInstance().getGame(Integer.parseInt(gameId));
			try 
			{
				if (!g.move(Integer.parseInt(legionId),Integer.parseInt(x),Integer.parseInt(y)))
					return "undoMove " + legionId; 
				return "moveOK " + x + " " + y;
			}catch(ActionPointException ape)
			{
				System.out.println(ape.getMessage());
				return "undoMove " + legionId;
			}
		}

		private String passTurn(String gameId) 
		{
			Game g = GameManager.getInstance().getGame(Integer.parseInt(gameId));
			if(g != null)
			{
				g.nextTurn();
				return "passOK " + g.getId();
			}
			return "gameOver";
		}

		private String joinGame() 
		{
			//GameManager.getInstance().subscribe( _connection );

			Game g = GameManager.getInstance().getLastGame();
			if ( g == null )
			{
				GameManager.getInstance().addGame( _connection );
				g = GameManager.getInstance().getLastGame();
			}
			
			return GameManager.getInstance().newPlayer( _connection );
		}
		
		@Override
		public void onOpen(Connection connection) 
		{
			this._connection = connection;
			System.out.println("Opened");   
		}

		@Override
		public void onClose(int closeCode, String message) 
		{
			System.out.println("Closed");   
		}
	}
}
